<template>
	<view class="logins">
		<nav-bar isBack  title="众筹夺宝" ></nav-bar>
		<!-- 顶部 -->
		<view class="i_cont">
			<view class="tit" v-if="Coupon">
				<image src="https://manager.ryz1620.com/static/my/icons.png"></image>
				<view>一元购买{{Coupon.discount_money}}元抵购券，赠送一次众筹夺宝机会</view>
			</view>
	         <view class="wrapper" v-if="Coupon">
	            <view class="left">
					<view class="pirce">￥<text>{{Coupon.discount_money}}</text></view>
					<view class="pircecom">满{{Coupon.condition}}可用</view>
				</view>
	            <view class="right">
					<view class="time">有效期间</view>
					<view class="time">{{Coupon.start_time | formatDate}} - {{Coupon.end_time |formatDate}} </view>
					<view class="nums"><text>{{num}}</text> 张</view>
				</view>
	            </view>
			 <view class="btn" @click="getOrder()" v-if="Coupon">{{num * Coupon.price}}元购买</view>	
		</view>
	</view>
</template>

<script>
import navBar from "@/components/navBar/navBar.vue"
import service_my from '@/service/my.js'
import service_mall from '@/service/mall.js'
import URL from '@/config/url.js'


export default {
	components: { navBar },
	
	data() {
		return {
			userId:"",
			Coupon:'',
			num:1,
			terminal:"",
		}
	},
	onLoad(params) {
		this.$utils.getUrl()
		if(params.num){
			this.num = params.num
		}
      this.getsaleCoupon()
	  //#ifdef MP-WEIXIN
	  	this.terminal = "wxapplet";
	  //#endif
	},
	filters: {
		formatDate(value) {
			let date = new Date(parseInt(value) * 1000);
			let y = date.getFullYear();
			let m = date.getMonth() + 1;
			m = m < 10 ? "0" + m : m;
			let d = date.getDate();
			d = d < 10 ? "0" + d : d;
			let h = date.getHours();
			h = h < 10 ? "0" + h : h;
			let minute = date.getMinutes();
			let second = date.getSeconds();
			minute = minute < 10 ? "0" + minute : minute;
			second = second < 10 ? "0" + second : second;
			return y +"-" + m + "-" + d;
		}
	},
	methods: {
		getsaleCoupon(){
			service_my.getsaleCoupon({
			})
				.then(res => {
					let data = res.data
					this.Coupon = data
				})
				.catch(err => {
				})
		},
		getOrder(){
			service_my.getCouponOrder({
				coupon_id:this.Coupon.id,
				num:this.num
			})
				.then(res => {
					let order_code = res.data.order_code
					this.getpayorder(order_code)	
				})
				.catch(err => {
				})
		},
		getpayorder(order_codes){
			uni.showLoading('正在打开微信支付..')
			service_mall.getpay({
				order_no:order_codes,
				type:'coupon_order',
				pay_type:1
				}).then(res=>{
				uni.hideLoading();
				if(res.code == 1){
					let data = res.data
						if(this.terminal == 'wxapplet'){
							uni.requestPayment({
								appId:res.data.appId,
								nonceStr: res.data.nonceStr,
								package: res.data.package,
								paySign: res.data.paySign,
								signType: res.data.signType,
								timeStamp: res.data.timeStamp,
								success: (suc) => {
									let pages = getCurrentPages() // 当前页面
									 let beforePage = pages[pages.length - 2] // 前一个页面
									uni.navigateBack({
										success: function() {
										        beforePage.onLoad(); // 执行前一个页面的onLoad方法
										  }
									})
								},
								fail: (err) => {
									uni.hideLoading();
									let pages = getCurrentPages() // 当前页面
									 let beforePage = pages[pages.length - 2] // 前一个页面
									uni.navigateBack({
										success: function() {
										        beforePage.onLoad(); // 执行前一个页面的onLoad方法
										  }
									})
								}
							});
						}else{
							uni.requestPayment({
								provider: 'wxpay',
								orderInfo: {
									"appid":res.data.appid,
									"partnerid": res.data.partnerid,
									"prepayid": res.data.prepayid,
									"noncestr": res.data.noncestr,
									"package": res.data.package,
									"timestamp": res.data.timestamp,
									"sign": res.data.sign,
								},
								success: (suc) => {
									uni.hideLoading();
									 let pages = getCurrentPages() // 当前页面
									  let beforePage = pages[pages.length - 2] // 前一个页面
									 uni.navigateBack({
									 	success: function() {
									 	        beforePage.onLoad(); // 执行前一个页面的onLoad方法
									 	  }
									 })
								},
								fail: (err) => {
									uni.hideLoading();
									let pages = getCurrentPages() // 当前页面
									 let beforePage = pages[pages.length - 2] // 前一个页面
									uni.navigateBack({
										success: function() {
										        beforePage.onLoad(); // 执行前一个页面的onLoad方法
										  }
									})
								}
							});
						}
						
				}else{
					uni.showToast({title: res.msg, icon: "none"});
				}	
			})
		},
	}
}
</script>

<style lang="scss" scoped>
.logins{
	width: 100vw;height: 100vh;background: #fff;display: flex;flex-flow: column;
}
.i_cont{
	position: relative;
	
}
.wrapper {
  display: flex;
  position: relative;
  height: 160rpx;
  margin: 30rpx;
  filter: drop-shadow(1px 1px 1px #ffced0);
}
.wrapper view{
  height: 100%;
}

.left {
  position: relative;
  background: #58a;
  background: radial-gradient(circle at top right, transparent 5px, #fff0f0 0) top right, radial-gradient(circle at bottom right, transparent 5px, #fff0f0 0) bottom right;
  background-size: 100% 60%;
  background-repeat: no-repeat;
  color: white;
  width: 32%;
  border-radius: 10rpx 0 0 10rpx;
}

.right {
  position: relative;
  background: #58a;
  background: radial-gradient(circle at top left, transparent 5px, #fffcfc 0) top left,radial-gradient(circle at bottom left, transparent 5px, #fffcfc 0) bottom left;
  background-size: 100% 60%;
  background-repeat: no-repeat;
  width: 68%;
  color: white;
  border-radius: 0 10rpx 10rpx 0; 
}
.right::after{
  content: "";
  position: absolute;
  top:12rpx;
  height: 140rpx;
  border-left: 1rpx dashed #ffced0;
}
.wrapper .pirce{
	font-size: 24rpx;
	font-weight: bold;
	color: #cc4e5e;
	line-height: 60rpx;
	height: 60rpx;
	text-align: center;
	margin-top: 25rpx;
	text{
		font-size: 32rpx;
	}
}
.wrapper .pircecom{
	font-size: 24rpx;
	color: #cc4e5e;
	line-height: 60rpx;
	height: 60rpx;
	text-align: center;
}
.wrapper .right{
	padding-top: 20rpx;
}
.wrapper .right .time{
    line-height: 60rpx;
	height:60rpx;
	font-size: 24rpx;
	margin-left: 20rpx;
	color: #666;
}
.wrapper .right .nums{
	position: absolute;
	right: 20rpx;
	line-height: 60rpx;
	height: 60rpx;
	top:40rpx;
	color: #cc4e5e;
	font-weight: bold;
	font-size: 24rpx;
	text{
		font-size: 36rpx;
	}
}
.tit{
	padding-top: 100rpx;
	padding-bottom: 50rpx;
	text-align: center;
	font-weight: bold;
	image{
		width: 120rpx;
		height: 120rpx;
		display: block;
		margin: auto;
		margin-bottom: 30rpx;
	}
}
.btn{
	width: 300rpx;
	line-height: 80rpx;
	text-align: center;
	color: #fff;
	background: #aca490;
	margin: 70rpx auto;
}
</style>